import pygame as p
import sys

# 初始化
chang, kuan = 702, 870
p.mixer.init()
p.init()
p.time.delay(100)
font = p.font.SysFont('arial', 25)
store_font = p.font.SysFont('arial', 50)
p.mixer_music.set_volume(0.1)
pos = [[150, 118], [91, 168], [115, 238], [196, 249], [217, 346], [303, 395], [331, 430], [343, 500], [420, 525],
       [354, 635], [350, 727], [488, 766], [406, 718], [466, 720], [459, 635], [541, 631], [516, 511], [574, 511],
       [628, 470], [608, 445], [527, 353], [410, 419], [406, 318], [331, 286], [242, 152], [411, 155], [507, 163]]
weather = ['2', '2', '1', '3', '1', '2', '3', '1', '2', '2', '3', '2', '1', '2', '2', '2', '3', '3', '2', '2', '1', '1',
           '2', '1', '3', '2', '1', '1', '2', '2']

score = 0
day = 0
money = 10000
food = 0
weight = 0
water = 0
mess_water = 3
mess_food = 2
money_water = 5
money_food = 10
cost_stay = [[5, 7], [8, 6], [10, 10]]  # 每天花费
cost_move = [[10, 14], [16, 12], [20, 20]]
in_store = False
diao_store = False
diao_kuang = False
store_water = 0
store_food = 0
store_money = 0
temp_money = 0
store_cun = False


def restar():
    global score, day, money, food, weight, water, in_store, diao_kuang, store_water, store_food, store_money, temp_money, diao_store, store_cun
    score = 0
    day = 0
    money = 10000
    food = 0
    weight = 0
    water = 0
    in_store = False
    diao_store = False
    diao_kuang = False
    store_water = 0
    store_food = 0
    store_money = 0
    temp_money = 0
    store_cun = False


# 处理位置#将位置转化成列表数字
for i in range(len(pos)):
    pos[i].append(i)

# 加载图片
screen = p.display.set_mode((chang, kuan))  # 地图屏幕
pic_store = p.image.load('store.png').convert_alpha()
pic_store_an = p.image.load('store_an.png').convert_alpha()
pic_bg = p.image.load('background.png').convert_alpha()
pic_store_l = p.image.load('store_l.png').convert_alpha()
pic_restart = p.image.load('restart.png').convert_alpha()
title = p.display.set_caption('穿越沙漠')
ren1 = p.image.load('ren1.png').convert_alpha()
ren2 = p.image.load('ren1.png').convert_alpha()
money_moutain = p.image.load('money.png').convert_alpha()
money_moutain_an = p.image.load('money_an.png').convert_alpha()


def shuju(weight, day, food, water, money):
    color = (0, 0, 0)
    wen_weight = font.render('weight:{}'.format(weight), True, color)
    wen_water = font.render('water:{}'.format(water), True, color)
    wen_day = font.render('day:{}'.format(day), True, color)
    wen_food = font.render('food:{}'.format(food), True, color)
    wen_money = font.render('money:{}'.format(money), True, color)
    allwen = [wen_weight, wen_water, wen_day, wen_food, wen_money]
    wenpos = [(10, 0), (270, 0), (540, 0), (10, 30), (240, 30)]
    ci = 0
    for i in allwen:
        screen.blit(i, (wenpos[ci]))
        ci += 1


# 人物对象
class P():
    def __init__(self):
        self.pos = [116, 52]  # 人物起点的坐标
        self.ren1 = ren1
        self.ren2 = ren2
        self.xuhao = 1

    def move(self, x, y):
        global day, money, water, food, weight
        dx = x - self.pos[0] - 20
        dy = y - self.pos[1] - 30
        # 将位置点的移动
        self.pos[0] += dx
        self.pos[1] += dy
        day += 1
        weight = weight - cost_thing()[0] * mess_water - cost_thing()[1] * mess_food
        water -= cost_thing()[0]
        food -= cost_thing()[1]

    def rester(self, x, y):
        self.pos = [116, 52]  # 人物起点的坐标
        self.ren1 = ren1
        self.ren2 = ren2
        self.xuhao = 1
        dx = x - self.pos[0] - 20
        dy = y - self.pos[1] - 30
        # 将位置点的移动
        self.pos[0] += dx
        self.pos[1] += dy

    def wakuang(self):
        global day, money, water, food, weight
        day += 1
        weight = weight - 3 / 2 * cost_thing()[0] * mess_water - 3 / 2 * cost_thing()[1] * mess_food
        water -= cost_thing()[0] * 3 / 2
        food -= cost_thing()[1] * 3 / 2
        money += 1000


renwu = P()


# 判断矿山的图标是否给出
def kuang():
    global diao_kuang
    if 344 - 20 <= renwu.pos[0] <= 535 + 90 and 770 - 20 <= renwu.pos[1] <= 790 + 50:
        screen.blit(money_moutain, (40, 600))
        diao_kuang = True
    else:
        screen.blit(money_moutain_an, (40, 600))
        diao_kuang = False


# 判断商店图标是否给出
def shang():
    global in_store, store_cun
    if 0 <= renwu.pos[0] <= 160 and 0 <= renwu.pos[1] <= 70:
        screen.blit(pic_store_l, (40, 500))
        in_store = True
        store_cun = False
    elif 397 <= renwu.pos[0] <= 468 and 566 <= renwu.pos[1] <= 618:
        screen.blit(pic_store_l, (40, 500))
        in_store = True
        store_cun = True
    else:
        screen.blit(pic_store_an, (40, 500))
        in_store = False
        store_cun = False


def get_exit(event):
    if event.type == p.QUIT:
        p.quit()
        sys.exit()


# 消耗
def cost_thing():
    global weight, day, food, water, money
    daliycost = cost_move[eval(weather[day - 1]) - 1]
    return daliycost


# 商店的窗体
def store_chuang():
    global water, weight, food, money, diao_store, store_water, store_food, store_money, temp_money, sotre_cun
    if temp_money == 0:
        store_money = money
        temp_money = 1
    screen_sotre = p.display.set_mode((755, 836))  # 商店屏幕
    screen_sotre.blit(pic_store, (0, 0))
    store_list = [store_water, store_food, store_money]
    store_pos_list = [[362, 190], [362, 390], [285, 772]]
    ci = 0
    for i in store_list:
        store_wen = store_font.render('{}'.format(str(i)), True, (0, 255, 200))
        screen_sotre.blit(store_wen, (store_pos_list[ci][0], store_pos_list[ci][1]))
        ci += 1
    for event in p.event.get():
        if event.type == p.MOUSEBUTTONDOWN:
            if store_cun == True:
                if 362 <= p.mouse.get_pos()[0] <= 482 and 138 <= p.mouse.get_pos()[1] <= 181:  # 水键上
                    store_water += 1
                    store_money = store_money - money_water * 2
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 125 <= p.mouse.get_pos()[1] <= 176:  # 水键上*50
                    store_water += 50
                    store_money = store_money - money_water * 50 * 2
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 235 <= p.mouse.get_pos()[1] <= 280:  # 水键下*50
                    store_water -= 50
                    store_money = store_money + money_water * 50 * 2
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 246 <= p.mouse.get_pos()[1] <= 284:  # 水键下
                    store_water -= 1
                    store_money = store_money + money_water * 2
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 335 <= p.mouse.get_pos()[1] <= 374:  # 食物键上
                    store_food += 1
                    store_money = store_money - money_food * 2
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 328 <= p.mouse.get_pos()[1] <= 375:  # 食物键上*50
                    store_food += 50
                    store_money = store_money - money_food * 50 * 2
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 442 <= p.mouse.get_pos()[1] <= 482:  # 食物键下
                    store_food -= 1
                    store_money = store_money + money_food * 2
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 435 <= p.mouse.get_pos()[1] <= 482:  # 食物键下*50
                    store_food -= 50
                    store_money = store_money + money_food * 50 * 2
                elif 13 <= p.mouse.get_pos()[0] <= 292 and 459 <= p.mouse.get_pos()[1] <= 645:  # sure
                    money = money - 2 * (store_water * money_water + store_food * money_food)  # 减去花费的钱
                    water += store_water
                    food += store_food
                    weight = weight + store_food * mess_food + store_water * mess_water  # 显示背包的质量
                    diao_store = False
                    temp_money = 0
                    store_water = 0
                    store_food = 0
                elif 459 <= p.mouse.get_pos()[0] <= 736 and 545 <= p.mouse.get_pos()[1] <= 631:  # cancel
                    store_water = 0
                    store_food = 0
                    temp_money = 0
            else:
                if 362 <= p.mouse.get_pos()[0] <= 482 and 138 <= p.mouse.get_pos()[1] <= 181:  # 水键上
                    store_water += 1
                    store_money = store_money - money_water
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 125 <= p.mouse.get_pos()[1] <= 176:  # 水键上*50
                    store_water += 50
                    store_money = store_money - money_water * 50
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 235 <= p.mouse.get_pos()[1] <= 280:  # 水键下*50
                    store_water -= 50
                    store_money = store_money + money_water * 50
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 246 <= p.mouse.get_pos()[1] <= 284:  # 水键下
                    store_water -= 1
                    store_money = store_money + money_water
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 335 <= p.mouse.get_pos()[1] <= 374:  # 食物键上
                    store_food += 1
                    store_money = store_money - money_food
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 328 <= p.mouse.get_pos()[1] <= 375:  # 食物键上*50
                    store_food += 50
                    store_money = store_money - money_food * 50
                elif 362 <= p.mouse.get_pos()[0] <= 482 and 442 <= p.mouse.get_pos()[1] <= 482:  # 食物键下
                    store_food -= 1
                    store_money = store_money + money_food
                elif 578 <= p.mouse.get_pos()[0] <= 650 and 435 <= p.mouse.get_pos()[1] <= 482:  # 食物键下*50
                    store_food -= 50
                    store_money = store_money + money_food * 50
                elif 13 <= p.mouse.get_pos()[0] <= 292 and 459 <= p.mouse.get_pos()[1] <= 645:  # sure
                    money = money - (store_water * money_water + store_food * money_food)  # 减去花费的钱
                    water += store_water
                    food += store_food
                    weight = weight + store_food * mess_food + store_water * mess_water  # 显示背包的质量
                    diao_store = False
                    temp_money = 0
                    store_water = 0
                    store_food = 0
                elif 459 <= p.mouse.get_pos()[0] <= 736 and 545 <= p.mouse.get_pos()[1] <= 631:  # cancel
                    store_water = 0
                    store_food = 0
                    temp_money = 0


def normal():
    global weight, day, food, water, money, chang, kuan, renwu
    screen = p.display.set_mode((chang, kuan))  # 地图屏幕
    screen.blit(pic_bg, (0, 0))
    screen.blit(pic_restart, (0, 800))
    shang()
    kuang()
    shuju(weight, day, food, water, money)
    screen.blit(renwu.ren1, (renwu.pos[0], renwu.pos[1]))


def sotp():  # 沙城暴
    global water, food, day, weight
    water -= cost_thing()[0] * 3
    food -= cost_thing()[1] * 3
    day += 1
    weight = weight - 3 * cost_thing()[0] * mess_water - 3 * cost_thing()[1] * mess_food


def gamevoer():  # 兑换成钱
    global money, water, food, money_food, money_water
    if water > 0:
        money += water * money_water / 2
    if food > 0:
        money += food * money_food / 2


# 循环
while True:
    if day == 31:
        p.quit()
        sys.exit()
    for event in p.event.get():
        get_exit(event)
        if event.type == p.MOUSEBUTTONDOWN:
            if 40 <= p.mouse.get_pos()[0] <= 140 and 500 <= p.mouse.get_pos()[1] <= 600:  # 激活商店
                diao_store = True
            elif diao_kuang == True and 42 <= p.mouse.get_pos()[0] <= 175 and 601 <= p.mouse.get_pos()[1] <= 713:
                renwu.wakuang()
            elif 9 <= p.mouse.get_pos()[0] <= 146 and 800 <= p.mouse.get_pos()[1] <= 856:  # 重新开始
                restar()
                renwu.rester(130, 70)
            elif int(weather[day]) == 3:
                sotp()
            elif 472 <= p.mouse.get_pos()[0] <= 572 and 98 <= p.mouse.get_pos()[1] <= 183:
                gamevoer()
            elif diao_store == False:
                print(p.mouse.get_pos())
                renwu.move(p.mouse.get_pos()[0], p.mouse.get_pos()[1])
    if diao_store == True and in_store == True:
        store_chuang()
    else:
        normal()
    p.display.update()
